home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 34 / Amiga Format CD34 (1998-11-20)(Future Publishing)(GB)[!][Christmas issue].iso / -seriously_amiga- / programming / c / viewperf5.1 / tools / convert.c next >
C/C++ Source or Header  |  1998-10-01  |  30KB  |  1,051 lines

  1. /*
  2. // Permission to use, copy, modify, and distribute this software and its
  3. // documentation for any purpose and without fee is hereby granted, provided
  4. // that the above copyright notice appear in all copies and that both that
  5. // copyright notice and this permission notice appear in supporting
  6. // documentation, and that the name of I.B.M. not be used in advertising
  7. // or publicity pertaining to distribution of the software without specific,
  8. // written prior permission. I.B.M. makes no representations about the
  9. // suitability of this software for any purpose.  It is provided "as is"
  10. // without express or implied warranty.
  11. //
  12. // I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
  14. // BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  16. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  17. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. //
  19. // DB Murrell
  20. // * 6/15/93
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <time.h>
  29. #include <sys/time.h>
  30. #include <sys/types.h>
  31. #include <sys/errno.h>
  32.  
  33. #define VERSION        "CONVERT Version 1.0 (6/15/93)"
  34. #define ERROR_OUT    stderr
  35. #define END_OF_TEXT      "### EOT ###"
  36. #define FLAGS        "hH?bBaAvVmMpPoOcC"
  37. #define N_POLY_TYPES    3
  38. #ifndef TRUE
  39.   #define TRUE 1
  40. #endif
  41. #ifndef FALSE
  42.   #define FALSE 0
  43. #endif
  44.  
  45. typedef int int32;              /* 32 bit integer */
  46. typedef unsigned int uint32;    /* 32 bit unsigned integer */
  47. typedef float float32;          /* 32 bit single precision float */
  48. typedef enum {False = 0, True}            Boolean;
  49. typedef enum {INFORMATION, WARNING, FATAL}    Severity;
  50. typedef enum {BINARY, ASCII}            Output_Mode;
  51. typedef enum {MESH, POLYGON}            Input_Type;
  52. typedef enum {COORDINATES = 0, ELEMENTS, NORMS}    Poly_Type;
  53.  
  54. static char    *self_name = NULL;
  55.  
  56. /*
  57.  * Polygon information table
  58.  */
  59. static struct {
  60.     Poly_Type    type;
  61.     char     *ext;
  62.     char    *desc;
  63. } poly_table[] = {
  64.     { COORDINATES, ".coor",  "Coordinate Data" },
  65.     { ELEMENTS,    ".elem",  "Element Data"    },
  66.     { NORMS,       ".vnorm", "Vector Normals"  }
  67. };
  68.  
  69. /*
  70.  * Help text
  71.  */
  72. static char *help_text[] = {
  73.     "Converts viewperf polygon and mesh files to and from binary",
  74.     "and ASCII formats",
  75.     "",
  76.     "USAGE:   convert [options] <input_file> <output_file>",
  77.     "",
  78.     "options:",
  79.     "   -[hH?]     Display this message",
  80.     "",
  81.     "   -[bB]      Convert to binary (assumes ASCII input)",
  82.     "",
  83.     "   -[aA]      Convert to ASCII  (assumes binary input)",
  84.     "",
  85.     "   -[vV]      Verbose mode",
  86.     "",
  87.     "   -[mM]      Input file is full path to viewperf mesh data",
  88.     "",
  89.     "   -[pPoO]    Input file is path prefix to viewperf polygon data",
  90.     "              (suffixes .coor, .elem, and .vnorm are added by convert)",
  91.     "",
  92.     "   -[cC]      Add comments to ASCII output files",
  93.     "",
  94.     "Input and output files default to stdin, and stdout (respectively) if",
  95.     "left unspecified",
  96.     END_OF_TEXT
  97. };
  98.  
  99. /*
  100.  ********************************************************************
  101.  *
  102.  * Swap32 -- Byte swap a 32 bit datum to convert between big
  103.  *           and little endian formats.
  104.  *
  105.  ********************************************************************
  106.  */
  107. static void Swap32(void *src_void, void *dst_void, size_t length)
  108. {
  109.   register uint32 tmp;
  110.   uint32 *src = (uint32 *) src_void;
  111.   uint32 *dst = (uint32 *) dst_void;
  112.   size_t i;
  113.  
  114.   for (i = 0; i < length; i++) {
  115.     tmp = src[i];
  116.     tmp &= 0xffffffff;
  117.     tmp = ((tmp >> 24) | (tmp << 24) | 
  118.            ((tmp >> 8) & 0xff00) | ((tmp << 8) & 0xff0000));
  119.     dst[i] = tmp;
  120.   }
  121. }
  122.  
  123. /*
  124.  ********************************************************************
  125.  *
  126.  * Error -- Error reporting
  127.  *
  128.  ********************************************************************
  129.  */
  130. static void Error(Severity sev, char *file, unsigned line, 
  131.        char *procedure, char *format, ...)
  132. {
  133.     char        level[16];
  134.     va_list     args;
  135.  
  136.     /*
  137.      * Sanity check
  138.      */
  139.     if (file == NULL || self_name == NULL || procedure == NULL)
  140.         return;
  141.  
  142.     va_start(args, format);
  143.  
  144.     switch (sev)
  145.     {
  146.     case INFORMATION:
  147.         strcpy(level, "NOTE");
  148.         break;
  149.     case WARNING:
  150.         strcpy(level, "*** WARNING");
  151.         break;
  152.     default:
  153.         strcpy(level, ">>> FATAL");
  154.         break;
  155.     }
  156.  
  157.     fprintf(ERROR_OUT, "%s ('%s'@%d, %s::%s) -\n\t", 
  158.         level, file, line, self_name, procedure);
  159. /*
  160.     if (format == NULL)
  161.         fprintf(ERROR_OUT, "%s", strerror(errno));
  162.     else
  163.         vfprintf(ERROR_OUT, format, args);
  164. */
  165.     fprintf(ERROR_OUT, "\n");
  166.     fflush(ERROR_OUT);
  167.  
  168.     va_end(args);
  169.  
  170.     if (sev == FATAL)
  171.         exit(1);
  172. } /* Error */
  173.  
  174. /*
  175.  ********************************************************************
  176.  *
  177.  * Expand -- Stretch a storage area to at least size (bytes) given
  178.  *
  179.  ********************************************************************
  180.  */
  181. static void Expand(char **store, size_t size)
  182. {
  183.     /*
  184.      * Sanity check
  185.      */
  186.     if (store == NULL || size == 0)
  187.     return;
  188.  
  189.     if ((*store) == NULL)
  190.     {
  191.         if (((*store) = (char *)malloc(size)) == NULL)
  192.             Error(FATAL, __FILE__, __LINE__, "Expand", NULL);
  193.     }
  194.     else if (((*store) = (char *)realloc(*store, size)) == NULL)
  195.         Error(FATAL, __FILE__, __LINE__, "Expand", NULL);
  196. } /* Expand */
  197.  
  198. /*
  199.  ********************************************************************
  200.  *
  201.  * Str_Dup -- Generate copy of string argument
  202.  *
  203.  ********************************************************************
  204.  */
  205. static void Str_Dup(char **dest, char *src)
  206. {
  207.     if (src == NULL)
  208.     {
  209.         if (*dest != NULL)
  210.             free(*dest);
  211.         *dest = NULL;
  212.         return;
  213.     } /* if */
  214.  
  215.     else if (*dest == NULL)
  216.     {
  217.         if ((*dest = (char *)malloc(strlen(src) + 1)) == NULL)
  218.         Error(FATAL, __FILE__, __LINE__, "Str_Dup", NULL);
  219.     } /* else if */
  220.  
  221.     else if ((*dest = (char *)realloc(*dest, strlen(src) + 1)) == NULL)
  222.     Error(FATAL, __FILE__, __LINE__, "Str_Dup", NULL);
  223.  
  224.     strcpy(*dest, src);
  225. } /* Str_Dup */
  226.  
  227. /*
  228.  ********************************************************************
  229.  *
  230.  * Str_Append -- Tack a list of strings onto the end of the first,
  231.  *         expanding the first if necessary.
  232.  *
  233.  ********************************************************************
  234.  */
  235. static void Str_Append(char **dest, ...)
  236. {
  237.     va_list     args;
  238.     char        *src;
  239.  
  240.     /*
  241.      * Sanity checks
  242.      */
  243.     if (dest == NULL)
  244.         return;
  245.  
  246.     va_start(args, dest);
  247.     while ((src = (char *)va_arg(args, char*)) != NULL)
  248.     {
  249.         if ((*dest) == NULL)
  250.         {
  251.         Expand(dest, (strlen(src) + 1) * sizeof(char));
  252.             strcpy(*dest, src);
  253.         } /* if */
  254.         else
  255.         {
  256.         Expand(dest, (strlen(*dest) + strlen(src) + 1) * sizeof(char));
  257.             strcat(*dest, src);
  258.         } /* else */
  259.     } /* while */
  260.     va_end(args);
  261. } /* Str_Append */
  262.  
  263. /*
  264.  ********************************************************************
  265.  *
  266.  * Show_Help_Text -- Text for help message
  267.  *
  268.  ********************************************************************
  269.  */
  270. static void Show_Help_Text(void)
  271. {
  272.     register unsigned i = 0;
  273.  
  274.     fprintf(stderr, "\t\t\t%s\n\n", VERSION);
  275.     while (strcmp(help_text[i], END_OF_TEXT) != 0)
  276.         fprintf(stderr, "%s\n", help_text[i++]);
  277. } /* Show_Help_Text */
  278.  
  279. /*
  280.  ********************************************************************
  281.  *
  282.  * Scan -- scan past comments and white space
  283.  *
  284.  ********************************************************************
  285.  */
  286. static void Scan(FILE *fp)
  287. {
  288.     int c;
  289.  
  290.     /*
  291.      * Sanity check
  292.      */
  293.     if (fp == NULL)
  294.         return;
  295.  
  296.     while (!feof(fp))
  297.     {
  298.     c = fgetc(fp);
  299.  
  300.         if (isspace(c))
  301.         continue;
  302.  
  303.     else if (c == '#')
  304.     {
  305.         while (((c = fgetc(fp)) != EOF) && (c != '\n'))
  306.         ;
  307.         continue;
  308.     } /* else if */
  309.  
  310.     else if (c != EOF)
  311.     {
  312.         ungetc(c, fp);
  313.         break;
  314.     } /* else */
  315.     } /* while */
  316.  
  317. } /* Scan */
  318.  
  319. /*
  320.  *************************************************************